//*********************************************************
//
//    Copyright (c) Microsoft. All rights reserved.
//    This code is licensed under the Apache License, Version 2.0.
//    THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF
//    ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY
//    IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR
//    PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.
//
//*********************************************************

namespace Microsoft.Research.DataLayer
{
    using System;
    using System.Drawing;
    using System.IO;

    /// <summary>
    /// Implementation of various converters.
    /// </summary>
    public class DataConverter
    {
        #region Convert To's
        /// <summary>
        /// Converts to image.
        /// </summary>
        /// <param name="dataObject">The data object.</param>
        /// <returns></returns>
        public static Image convertToImage(DataObject dataObject)
        {
            Image image;
            if (dataObject.Data == null)
            {
                image = Image.FromStream(new MemoryStream(dataObject.DataBytes));
            }
            else
            {
                image = Image.FromStream(dataObject.Data);
                dataObject.Data.Close();
            }
            return image;
        }

        /// <summary>
        /// Converts to string.
        /// </summary>
        /// <param name="dataObject">The data object.</param>
        /// <returns></returns>
        public static string convertToString(DataObject dataObject)
        {
            Stream stream = dataObject.Data;
            if (stream == null)
            {
                // convert this to string and return
                return dataObject.DataBytes != null ? BitConverter.ToString(dataObject.DataBytes) : null;
            }
            else
            {
                return new StreamReader(stream).ReadToEnd();
            }

        }

        /// <summary>
        /// Converts to int32.
        /// </summary>
        /// <param name="dataObject">The data object.</param>
        /// <returns></returns>
        public static int convertToInt32(DataObject dataObject)
        {
            Stream stream = dataObject.Data;
            if (stream == null)
            {
                // convert this to string and return
                return dataObject.DataBytes != null ? BitConverter.ToInt32(dataObject.DataBytes, 0) : -1;
            }
            else
            {
                int value;
                int.TryParse((new StreamReader(stream).ReadToEnd()), out value);

                return value;
            }
        }

        /// <summary>
        /// Converts to URI.
        /// </summary>
        /// <param name="dataObject">The data object.</param>
        /// <returns></returns>
        public static Uri convertToUri(DataObject dataObject)
        {
            Stream stream = dataObject.Data;
            if (stream == null)
            {
                // convert this to string and return
                return dataObject.DataBytes != null ? new Uri(BitConverter.ToString(dataObject.DataBytes)) : null;
            }
            else
            {
                return new Uri(new StreamReader(stream).ReadToEnd());
            }
        }
        #endregion

        #region Convert From's
        /// <summary>
        /// Froms the object.
        /// </summary>
        /// <param name="src">The SRC.</param>
        /// <returns></returns>
        public static byte[] FromObject(object src)
        {
            System.Runtime.Serialization.Formatters.Binary.BinaryFormatter bf =
                new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
            MemoryStream srcOut = new MemoryStream();

            bf.Serialize(srcOut, src);

            return srcOut.GetBuffer();
        }

        /// <summary>
        /// Froms the image.
        /// </summary>
        /// <param name="src">The SRC.</param>
        /// <returns></returns>
        public static byte[] FromImage(Image src)
        {
            System.IO.MemoryStream saveStore = new System.IO.MemoryStream();
            src.Save(saveStore, System.Drawing.Imaging.ImageFormat.Png);
            return saveStore.GetBuffer();
        }
        #endregion
    }
}
